import { IDeptVO } from '@/api/admin/dept';
import { IUserVO, pageList } from '@/api/admin/user';
import { ref } from 'vue';

export function useUser() {
	const userPageSize = 20;
	const userCurrentPage = ref(1);
	const keyword = ref('');
	const userList = ref<IUserVO[]>([]);
	const userLoading = ref(false);
	const deptId = ref('');
	const total = ref(0);
	const userScrollbar = ref();

	async function fetchUser() {
		userLoading.value = true;
		const res = await pageList({
			deptId: deptId.value,
			name: keyword.value,
			current: userCurrentPage.value,
			size: userPageSize,
		});
		total.value = res.data.total;
		userList.value = res.data.records;
		userLoading.value = false;
		userScrollbar.value.setScrollTop(0);
	}

	function handleSearch() {
		deptId.value = '';
		userCurrentPage.value = 1;
		fetchUser();
	}

	function handleClickDept(data: IDeptVO) {
		keyword.value = '';
		userCurrentPage.value = 1;
		deptId.value = data.id;
		fetchUser();
	}

	return {
		userList,
		userLoading,
		userCurrentPage,
		userPageSize,
		keyword,
		deptId,
		total,
		userScrollbar,
		fetchUser,
		handleSearch,
		handleClickDept,
	};
}
